from django.template import Library
from web import models
from django.urls import reverse

register = Library()


# ''' 字符串自动补齐-问题列表的id，例如 #001，#002，#003 '''
@register.simple_tag
def string_completion(num):
    if num < 100:
        num = str(num).rjust(3, "0")
    return "#{}".format(num)

@register.simple_tag
def bytes_transition(bytes):
    if bytes >= 1024 * 1024 * 1024:
        return "%.2f GB" % (bytes / (1024 * 1024 * 1024),)
    elif bytes >= 1024 * 1024:
        return "%.2f MB" % (bytes / (1024 * 1024),)
    elif bytes >= 1024:
        return "%.2f KB" % (bytes / 1024)
    else:
        return "%d B" % bytes
